package com.jiyun.service;

import com.jiyun.dao.EmployeeMapper;
import com.jiyun.entity.City;
import com.jiyun.entity.Dept;
import com.jiyun.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper mapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private CityService cityService;

    public List<Employee> getAll(){
        List<Employee> employees = mapper.selectAll();
        for (Employee l : employees) {
            Dept dept = deptService.getDept(l.getDid());
            l.setDept(dept);
            City city = cityService.getCity(l.getCid());
            l.setCity(city);
        }
        return employees;
    }

    public Employee getEmployee(int id){
        return mapper.selectByPrimaryKey(id);
    }

    public void addEmployee(Employee employee){
        mapper.insert(employee);
    }

    public void updateEmployee(Employee employee){
        mapper.updateByPrimaryKey(employee);
    }

    public void deleteEmployee(int id){
        mapper.deleteByPrimaryKey(id);
    }

}
